package net.eicp.jxcontent.core.macroimpl

import scala.reflect.macros.blackbox.Context

object JXCopy {
  import scala.language.experimental.macros
  def apply[T <: JXCopyAble]: T = macro copyImpl[T]
  def copyImpl[T: c.WeakTypeTag](c: Context): c.Expr[T] = {
    import c.universe._
    val objectClass = symbolOf[T]
    if(! objectClass.isAbstract) {
      c.Expr[T](q"""{
        def copyObj:$objectClass  = new $objectClass()  {
          override def copyObject: this.type = copyObj.asInstanceOf[this.type]
        }
        copyObj
      }""")
    } else {
      throw new Exception(s"被实现的类${objectClass}不能是抽象的")
    }
  }
}

trait JXCopyAble {
  
  def copyObject: this.type = throw new Exception("JXDispatchSnippet中copyObject方法还没有子类的实现，请在appendSnippetDispatch的时候用  JXCopy[类名]  代替")
  
}